{
 "metadata": {
  "name": "srs_DG645"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "GPIB-USB Communication Library Examples"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Stanford Research Systems DG645 Digital Delay Generator"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "In this example, we will demonstrate how to connect to an SRS DG645 digital delay generator, and how to set a new delay pattern."
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We start by importing the `srs` package from within the main `instruments` package, along with the `quantities` package\n",
      "that is used to track physical quantities."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from instruments.srs import SRSDG645\n",
      "import quantities as pq"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Next, we open the instrument, assuming that it is connected via GPIB. Note that you may have to change this line\n",
      "to match your setup."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ddg = SRSDG645.open_gpibusb('/dev/ttyUSB0', 15)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "AttributeError",
       "evalue": "'NoneType' object has no attribute 'terminator'",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-3-d987b8421bbc>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mddg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mSRSDG645\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mopen_gpibusb\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'/dev/ttyUSB0'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m15\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[1;32mC:\\Users\\cgranade\\AppData\\Local\\Enthought\\Canopy\\User\\lib\\site-packages\\instruments\\abstract_instruments\\instrument.pyc\u001b[0m in \u001b[0;36mopen_gpibusb\u001b[1;34m(cls, port, gpib_address, timeout, writeTimeout)\u001b[0m\n\u001b[0;32m    294\u001b[0m                 \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    295\u001b[0m                  writeTimeout=writeTimeout)\n\u001b[1;32m--> 296\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgi_gpib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGPIBWrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mser\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgpib_address\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    297\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    298\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;32mC:\\Users\\cgranade\\AppData\\Local\\Enthought\\Canopy\\User\\lib\\site-packages\\instruments\\abstract_instruments\\gi_gpib.pyc\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, filelike, gpib_address)\u001b[0m\n\u001b[0;32m     49\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_terminator\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     50\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_eoi\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 51\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_file\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mterminator\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'\\r'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     52\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_strip\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     53\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
        "\u001b[1;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'terminator'"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Serial connection error. Connection not added to serial                 manager. Error message:None\n"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "We can now set the delay on channel $A$ to be $B + 10\\ \\mu\\text{s}$."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ddg.channel[ddg.Channels.A].delay = (ddg.Channels.B, pq.Quantity(10, 'us'))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "ename": "NameError",
       "evalue": "name 'ddg' is not defined",
       "output_type": "pyerr",
       "traceback": [
        "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
        "\u001b[1;32m<ipython-input-5-5c0f7101522f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mddg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchannel\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mddg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mChannels\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdelay\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mddg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mChannels\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mB\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpq\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mQuantity\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'us'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
        "\u001b[1;31mNameError\u001b[0m: name 'ddg' is not defined"
       ]
      }
     ],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}